﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Search.TxMainSearchControl.TxSearchItemCommand
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Search.TxSearchCommand.Resources;
using EngineeringInternalExtension;
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering;
using UiInternalControls.WPFControls.SearchControl;


namespace DnProcessSimulateCommands.Search.TxMainSearchControl;

public class TxSearchItemCommand : TxSearchItemBase, INotifyPropertyChanged
{
  private ImageSource _icon;
  private bool _isEnabled;
  private string _name = string.Empty;
  private const string _searchCommandID = "DnProcessSimulateCommands.Search.TxSearchCommand.CApSearchCommandCmd";
  private const string _regKeepWindowOpenKey = "Software\\Tecnomatix\\TUNE\\NewAssembler\\Commands Info\\DnProcessSimulateCommands\\Dialogs\\CUiSearchCommandWindow";
  private const string _regKeepWindowOpenName = "Keep Search Window Open";
  private static bool _wasRegKeepWindowOpenKeyLoaded;
  private static bool _keepWindowOpen;
  private string ARROWSTRING;

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanges(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }

  public virtual ImageSource Icon
  {
    get
    {
      if (this._icon == null)
      {
        System.Drawing.Icon commandIcon = new TxCommandManager().GetCommandIcon(this.ID);
        if (commandIcon != null)
        {
          this._icon = (ImageSource) System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(commandIcon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
          commandIcon.Dispose();
        }
      }
      return this._icon;
    }
    set
    {
    }
  }

  public virtual string Caption
  {
    set
    {
    }
    get
    {
      return !string.IsNullOrEmpty(this.Category) ? this.Category + this.ARROWSTRING + this.Name : this.Name;
    }
  }

  public string ID { get; set; }

  public string Name
  {
    get => this._name;
    set
    {
      this._name = value;
      if (!this._name.StartsWith("&"))
        return;
      this._name = this._name.TrimStart('&');
    }
  }

  public string Category { get; set; }

  public List<string> Alias { get; set; }

  public virtual bool IsEnabled
  {
    set
    {
      if (this._isEnabled == value)
        return;
      this._isEnabled = value;
      this.OnPropertyChanges(nameof (IsEnabled));
      this.OnPropertyChanges("Icon");
    }
    get => this._isEnabled;
  }

  public virtual string Tooltip { get; set; }

  public TxSearchItemCommand(
    string name,
    string id,
    string category = "",
    string tooltip = "",
    List<string> alias = null)
  {
    this.Name = name;
    this.ID = id;
    this.Category = category;
    base.Tooltip = tooltip;
    this.Alias = alias;
    this.ARROWSTRING = $" {char.ConvertFromUtf32(8594)} ";
    this.UpdateIsCommandEnabled();
  }

  public virtual void SelectionChanged()
  {
    if (!base.IsEnabled)
      return;
    if (this.IsLicensed())
    {
      TxApplicationEx.SetFocusToMainWindow();
      new TxCommandManager().ExecuteCommandByCommandProgId(this.ID, (object) null, false);
      if (this.ShouldKeepSearchWindowOpen())
        return;
      base.SelectionChanged();
    }
    else
    {
      int num = (int) TxMessageBox.ShowModal(TxSearchCommandStringTable.Missing_License_Message, TxSearchCommandStringTable.Name, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
  }

  public bool SearchInAlias(string str)
  {
    if (this.Alias == null)
      return false;
    str = TxSearchModelUtils.CleanStringForSearch(str);
    foreach (string alia in this.Alias)
    {
      if (alia.ToLower().Contains(str))
        return true;
    }
    return false;
  }

  internal void UpdateIsCommandEnabled()
  {
    TxCommandManager txCommandManager = new TxCommandManager();
    bool flag1 = false;
    bool flag2 = txCommandManager.IsCommandEnabled(this.ID, false);
    if (flag2)
      flag1 = txCommandManager.IsExecutableCommand(this.ID);
    if (flag1)
      flag1 = !(this.ID == "DnProcessSimulateCommands.Search.TxSearchCommand.CApSearchCommandCmd");
    base.IsEnabled = flag2 & flag1;
  }

  private bool ShouldKeepSearchWindowOpen()
  {
    if (!TxSearchItemCommand._wasRegKeepWindowOpenKeyLoaded)
    {
      TxSearchItemCommand._wasRegKeepWindowOpenKeyLoaded = true;
      int result = 0;
      RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\Tecnomatix\\TUNE\\NewAssembler\\Commands Info\\DnProcessSimulateCommands\\Dialogs\\CUiSearchCommandWindow");
      if (registryKey != null)
      {
        object obj = registryKey.GetValue("Keep Search Window Open");
        if (obj != null)
          int.TryParse(obj.ToString(), out result);
      }
      TxSearchItemCommand._keepWindowOpen = result == 1;
    }
    return TxSearchItemCommand._keepWindowOpen;
  }

  private static Bitmap SetBrightness(Bitmap bmp, int brightness)
  {
    Bitmap bitmap = (Bitmap) bmp.Clone();
    if (brightness < -255)
      brightness = -255;
    if (brightness > (int) byte.MaxValue)
      brightness = (int) byte.MaxValue;
    for (int x = 0; x < bitmap.Width; ++x)
    {
      for (int y = 0; y < bitmap.Height; ++y)
      {
        System.Drawing.Color pixel = bitmap.GetPixel(x, y);
        if (pixel.B != (byte) 0 || pixel.G != (byte) 0 || pixel.R != (byte) 0 || pixel.A != (byte) 0)
        {
          int red = (int) pixel.R + brightness;
          int green = (int) pixel.G + brightness;
          int blue = (int) pixel.B + brightness;
          if (red < 0)
            red = 1;
          if (red > (int) byte.MaxValue)
            red = (int) byte.MaxValue;
          if (green < 0)
            green = 1;
          if (green > (int) byte.MaxValue)
            green = (int) byte.MaxValue;
          if (blue < 0)
            blue = 1;
          if (blue > (int) byte.MaxValue)
            blue = (int) byte.MaxValue;
          bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb((int) (byte) red, (int) (byte) green, (int) (byte) blue));
        }
      }
    }
    return bitmap;
  }

  private static Bitmap ConvertToGrayScaleImage(Bitmap original)
  {
    Bitmap grayScaleImage = new Bitmap(original.Width, original.Height);
    Graphics graphics = Graphics.FromImage((Image) grayScaleImage);
    ColorMatrix newColorMatrix = new ColorMatrix(new float[5][]
    {
      new float[5]{ 0.3f, 0.3f, 0.3f, 0.0f, 0.0f },
      new float[5]{ 0.59f, 0.59f, 0.59f, 0.0f, 0.0f },
      new float[5]{ 0.11f, 0.11f, 0.11f, 0.0f, 0.0f },
      new float[5]{ 0.0f, 0.0f, 0.0f, 1f, 0.0f },
      new float[5]{ 0.0f, 0.0f, 0.0f, 0.0f, 1f }
    });
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetColorMatrix(newColorMatrix);
    graphics.DrawImage((Image) original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, imageAttr);
    graphics.Dispose();
    return grayScaleImage;
  }

  private bool IsLicensed() => new TxCommandManager().IsCommandLicensed(this.ID);
}
